package com.ctgu.edu.controller;

import com.ctgu.edu.pojo.*;
import com.ctgu.edu.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;

@Controller
public class RecruiterController {

    @Autowired
    @Qualifier("recruiterServiceImpl")
    private RecruiterServiceImpl recruiterServiceImpl;
    @Autowired
    @Qualifier("jobServiceImpl")
    private JobServiceImpl jobServiceImpl;
    @Autowired
    @Qualifier("applyServiceImpl")
    private ApplyServiceImpl applyServiceImpl;
    @Autowired
    @Qualifier("resumeServiceImpl")
    private ResumeServiceImpl resumeServiceImpl;
    @Autowired
    @Qualifier("SeekerServiceImpl")
    private SeekerServiceImpl seekerServiceImpl;

    //求职者注册
    @RequestMapping("/recruiter/create/job")
    public String CreateJob(Job job,Model model, HttpServletRequest request) {

        HttpSession session = request.getSession();
        Recruiter recruiter =(Recruiter) session.getAttribute("recruiter");
        job.setJobRecId(recruiter.getRecruiterId());
        job.setJobClickTimes(0);
        jobServiceImpl.addJob(job);

        System.out.println(job.toString());
        model.addAttribute("inf_recruiter","添加工作成功");

        return "forward:/recruiter/mainpage";
    }

    @RequestMapping("/recruiter/mainpage")
    public String RecruiterMainpage(Model model, HttpServletRequest request) {

        HttpSession session = request.getSession();
        Recruiter recruiter =(Recruiter) session.getAttribute("recruiter");
        Integer recId = recruiter.getRecruiterId();
        LinkedList<Job> jobs = jobServiceImpl.getJobByRecId(recId);

        model.addAttribute("jobs",jobs);

        return "recruiter_main_page";
    }


    //查看申请
    @RequestMapping("/recruiter/apply/{jobId}")
    public String LookJobApply(@PathVariable Integer jobId, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Recruiter recruiter =(Recruiter) session.getAttribute("recruiter"); //获取当前登录的用户

        LinkedList<Apply> applys = applyServiceImpl.getApplyByJobId(jobId);
        LinkedList<ApplySeekerInf> applySeekerInfs = new LinkedList<ApplySeekerInf>();
        Seeker tempseeker;
        for (Apply apply : applys) {
            tempseeker = seekerServiceImpl.getSeekerById(apply.getApplySeekerId());
            ApplySeekerInf applySeekerInf = new ApplySeekerInf(apply,tempseeker);
            applySeekerInfs.add(applySeekerInf);
        }


        model.addAttribute("applySeekerInfs",applySeekerInfs);
        return "job_applyer_page";
    }

    @RequestMapping("/recruiter/resume/{applySeekerId}/{applyJobId}")
    public String LookJobApplyer(@PathVariable("applySeekerId") Integer applySeekerId,@PathVariable("applyJobId") Integer applyJobId, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        Recruiter recruiter =(Recruiter) session.getAttribute("recruiter"); //获取当前登录的用户

        Resume resume;
        Seeker seeker;
        resume = resumeServiceImpl.getResumeByOwnerId(applySeekerId);
        seeker = seekerServiceImpl.getSeekerById(applySeekerId);
        model.addAttribute("seeker",seeker);
        model.addAttribute("resume",resume);
        model.addAttribute("applyJobId",applyJobId);

        return "applyer_resume";
    }


    @RequestMapping("/recruiter/apply/manage/{manage}/{applyJobId}/{seekerId}")
    public String ManageApply(@PathVariable("manage") String manage,@PathVariable("applyJobId") Integer applyJobId,@PathVariable("seekerId") Integer seekerId, Model model, HttpServletRequest request){

        Apply apply;
        apply = applyServiceImpl.getApplyBySeekerAndJobId(seekerId,applyJobId);
        if(manage.equals("accept")){
            apply.setApplyFlag(1);
        }else{
            apply.setApplyFlag(-1);
        }
        applyServiceImpl.updateApply(apply);
        String jumpUrl = "forward:/recruiter/apply/"+applyJobId;

        return jumpUrl;
    }

    @RequestMapping("/recruiter/job/edit/{jobId}")
    public String ManageApply(Job job,@PathVariable("jobId") Integer jobId,Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        Recruiter recruiter =(Recruiter) session.getAttribute("recruiter");
        job.setJobId(jobId);
        job.setJobRecId(recruiter.getRecruiterId());
        jobServiceImpl.updateJob(job);
        model.addAttribute("inf_recruiter","修改工作成功");
        return "forward:/recruiter/mainpage";
    }


    @RequestMapping("/recruiter/jump/jobedit/{jobId}")
    public String JumpJobEdit(@PathVariable("jobId") Integer jobId,Model model){
        model.addAttribute("jobId",jobId);
        return "edit_job";
    }


    @RequestMapping("/recruiter/job/delete/{jobId}")
    public String DeleteJob(@PathVariable("jobId") Integer jobId,Model model){
        jobServiceImpl.deleteJobById(jobId);
        model.addAttribute("inf_recruiter","删除工作成功");
        return "forward:/recruiter/mainpage";
    }

}
